package com.itgone.view.fragment;

import android.content.Intent;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.andview.refreshview.XRefreshView;
import com.github.lazylibrary.util.ToastUtils;
import com.itgone.R;
import com.itgone.bean.Product;
import com.itgone.presenter.IProductListPresenter;
import com.itgone.presenter.impl.ProductListPresenterImpl;
import com.itgone.view.IProductListView;
import com.itgone.view.activity.LoginActivity;
import com.itgone.view.adapter.HomeRVAdapter;

import java.util.ArrayList;
import java.util.List;

public class HomeFragment extends TabFragment implements IProductListView {

    private RecyclerView mRecyclerView;
    private IProductListPresenter mProductListPresenter;
    private HomeRVAdapter mHomeRVAdapter;
    private XRefreshView xRefreshView;

    public HomeFragment() {
        setTitle("首页");
    }

    @Override
    protected void initView() {
        mRecyclerView = (RecyclerView) findViewById(R.id.rv);
        xRefreshView = (XRefreshView) findViewById(R.id.xrv);
    }

    @Override
    protected void initData() {
        // 初始化 ProductListPresenter
        mProductListPresenter = new ProductListPresenterImpl(this);
        //初始化 RefreshView 相关
        xRefreshView.setXRefreshViewListener(mProductListPresenter);
        xRefreshView.setScrollBackDuration(300);
        xRefreshView.setPullRefreshEnable(false);
        xRefreshView.setPullLoadEnable(true);
        // 初始化 RecyclerView
        GridLayoutManager gridLayoutManager = new GridLayoutManager(getActivity(), 2);
        mRecyclerView.setLayoutManager(gridLayoutManager);
        mHomeRVAdapter = new HomeRVAdapter(getActivity());
        mHomeRVAdapter.setDataList(new ArrayList<Product>());

        mHomeRVAdapter.setOnItemClickListener(mProductListPresenter);
        mHomeRVAdapter.setOnItemLongClickListener(mProductListPresenter);

        mRecyclerView.setAdapter(mHomeRVAdapter);

        TextView textView = (TextView) findViewById(R.id.tv_home_page);
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent = new Intent(getActivity(), LoginActivity.class);
                startActivity(intent);
            }
        });

        mProductListPresenter.listProduct(null, null);
    }

    @Override
    int getRootViewId() {
        return R.layout.fragment_home;
    }

    @Override
    public List<Product> getProductList() {
        return mHomeRVAdapter.getProductList();
    }

    @Override
    public void notifyProductList(List<Product> productList) {
        if (productList == null) {
            ToastUtils.showToast(getActivity(), "数据返回为空");
            return;
        }

        mHomeRVAdapter.setDataList(productList);
        mHomeRVAdapter.notifyDataSetChanged();
    }

    @Override
    public void stopRefresh() {
        xRefreshView.stopRefresh();
    }

    @Override
    public void stopLoadMore() {
        xRefreshView.stopLoadMore();
    }

}
